/* 
 * shamefully copy from AnFetion.
 * more see: http://code.google.com/p/anfetion/
 */
package person.bangbang.im.Androidgin.Fetion;

import java.util.ArrayList;
import person.bangbang.im.Androidgin.Util.Log;

public class SendSMS extends SIPCBase {

    private String sendto;
    private String message;
    private boolean isUriTel;
    private FetionUri fetionid;
    public int status;

    public SendSMS(SerialManager serialManager, TcpSendAndReceive connection, FetionUri fetionid,
            String sendto, String message/* ,boolean tel *//* Tiger.Omit */) {
    	super(serialManager, connection);
        Log.d("sendSms", "call super constructor");
        this.fetionid = fetionid;
        this.sendto = sendto;
        this.message = message;
        this.isUriTel = sendto.startsWith("tel"); // tel; //Tiger.Modify
    }

    @Override
    public boolean execute() {
        onStatusChanged(StatusDefine.GOTFRIENDS, StatusDefine.SENDINGSMS);
        ArrayList<FetionSIPC.HeaderPair> headers = new ArrayList<FetionSIPC.HeaderPair>();

        headers.add(new FetionSIPC.HeaderPair("F", fetionid.getId()));
        headers.add(new FetionSIPC.HeaderPair("I", serialManager.getISerial() + " "));
        headers.add(new FetionSIPC.HeaderPair("Q", String.valueOf(serialManager.getMSerial()) + " M"));
        headers.add(new FetionSIPC.HeaderPair("T", sendto));

        if (!isUriTel) {
            headers.add(new FetionSIPC.HeaderPair("C", "text/plain"));
            headers.add(new FetionSIPC.HeaderPair("K", "SaveHistory"));
        } else {
            headers.add(new FetionSIPC.HeaderPair("N", "SendSMS"));
        }
        headers.add(new FetionSIPC.HeaderPair("L", StrUtil.getLength(message)));

        FetionSIPC sipc = new FetionSIPC("M fetion.com.cn SIP-C/2.0", headers, message);
        if (this.SendAndReceiveResponse(sipc)) {
            try {
                FetionSIPC.ResponseCmdLine cmd = sipc.response.getResponseCmdLine();

                status = StrUtil.parseInt(cmd.code, -1);
                // System.out.println(cmd.code + "=====" + status);
                if (!cmd.code.equals(SIPC_RESPONSE_CODE_SENDSMS_OK)
                        && !cmd.code.equals(SIPC_RESPONSE_CODE_OK)) {
                    this.onSetText("Sending SMS error, the error message is:" + cmd.description);
                    onStatusChanged(StatusDefine.SENDINGSMS, StatusDefine.GOTFRIENDS);
                    return false;
                } else {
                    onStatusChanged(StatusDefine.SENDINGSMS, StatusDefine.GOTFRIENDS);
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
                this.onSetText("Sending SMS error, the error message is:" + e.getMessage());
                onStatusChanged(StatusDefine.SENDINGSMS, StatusDefine.GOTFRIENDS);
                return false;
            }
        } else {
            this.onSetText("Time out while sending SMS,please retry.");
            onStatusChanged(StatusDefine.SENDINGSMS, StatusDefine.GOTFRIENDS);
            return false;
        }
    }

}
